package com.oceansoft.mobile.econsole.modules.api.v2.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.api.entity.TskOrderInfo;
import com.oceansoft.mobile.econsole.modules.api.v2.service.IProjectService;
import com.oceansoft.mobile.econsole.modules.ascproject.entity.ProjectInfo;
import com.oceansoft.mobile.econsole.modules.ascproject.persistence.IProjectMapper;
import com.oceansoft.mobile.econsole.modules.base.entity.ApiPager;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.jlwt.entity.IstPascMonitorApply;
import com.oceansoft.mobile.econsole.modules.jlwt.entity.MonitorApplySP;
import com.oceansoft.mobile.econsole.modules.watchlog.persistence.IWatchLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * Created by wzy on 2016/2/1.
 */
@Service
public class ProjectServiceImpl implements IProjectService {
    @Resource
    IProjectMapper projectMapper;

    @Resource
    IWatchLogMapper watchLogMapper;

    @Resource
    ConfigInfo configInfo;


    @Override
    public Pager getPrjTh(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ProjectInfo> page = (Page<ProjectInfo>) projectMapper.getPrjTh(query);
        return PageUtil.page2pager(page, query);
    }


    @Override
    @Transactional
    public Result unDoPrj(String prjBillId) {

        try {

            IstPascMonitorApply apply = projectMapper.getMonitorApply(prjBillId);

            if (apply.getDocStatus() != "1") {
                return new Result(Result.SUCC, "办件不能撤销!");
            }
            MonitorApplySP ent = new MonitorApplySP();
            ent.setId(UUID.randomUUID().toString());
            ent.setCheckNode("-2");
            ent.setCreateTime(new Date());
            ent.setDocNo(prjBillId);

            ent.setCheckDeptCode(apply.getUserCode());
            ent.setCheckDeptName(apply.getAppUserName());
            ent.setCheckPerson(apply.getAppUserName());
            ent.setCheckResult("撤销");
            ent.setCheckRemark("撤销");
            ent.setCheckTime(new Date());


            watchLogMapper.insertMonitorFlow(ent);

            projectMapper.unDoPrj(prjBillId);

            projectMapper.unDoPrjStatus(prjBillId);

            projectMapper.updateApplyStatus("-2", prjBillId);
        } catch (Exception ex) {

            return new Result(Result.FAILURE, "撤销失败!");
        }
        return new Result(Result.SUCC, "撤销成功!");
    }

    @Override
    public Result queryMyOrders(Query query) {
        int pageNum = query.getPageNum();
        int numPerPage = query.getNumPerPage();
        String status = query.getProjectstatus();
        String loginId = query.getLoginId();
        //        loginId = "f8ccea15e0d941999a15bbc95d9c8bb8";

        String url = configInfo.getServiceInterfaceAddress()
                + "/reservation/myorder/page"
                + "?pi=" + pageNum
                + "&ps=" + numPerPage
                + "&q_creator_s_eq=" + loginId
                + "&q_state_s_eq=" + status;
        ApiPager<TskOrderInfo> apiPager = HttpClientUtil.doGet(url, TskOrderInfo.class);
        Result result = new Result();
        result.setSucc(Result.SUCC);
        result.setData(apiPager);
        result.setMsg("");
        result.setHasNext(apiPager.isHasNext());
        return result;
    }

    @Override
    public Result queryOrderDetail(String orderId) {
        Result result = new Result(false);
        String url = configInfo.getServiceInterfaceAddress() + "/reservation/myorder/" + orderId;
        String res;
        try {
            res = HttpClientUtil.doGet(url);
            Objects.requireNonNull(res, "获取接口数据失败");
        } catch (IOException e) {
            result.setData("获取接口数据失败");
            return result;
        }

        JSONObject jsonObject = JSON.parseObject(res);
        boolean succ = jsonObject.getBooleanValue("succ");
        result.setSucc(succ);
        if (succ) {
            TskOrderInfo orderInfo = jsonObject.getObject("data", TskOrderInfo.class);
            result.setData(orderInfo);
        }else {
            result.setMsg(jsonObject.getString("msg"));
        }

        return result;
    }

    @Override
    public Result revoke(String orderId) {
        String url = configInfo.getServiceInterfaceAddress() + "/reservation/myorder/" + orderId;
        String res = HttpClientUtil.delete(url);
        Result result = JSON.parseObject(res, Result.class);
        return result;
    }

    @Override
    public ProjectInfo getPrjInfo(String prjBillId) {
        return projectMapper.getPrjInfo(prjBillId);
    }

    @Override
    public Pager getPrjYbj(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ProjectInfo> page = (Page<ProjectInfo>) projectMapper.getPrjYbj(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Pager getPrjBlz(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ProjectInfo> page = (Page<ProjectInfo>) projectMapper.getPrjBlz(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Pager getPrjYs(Query query) {

        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ProjectInfo> page = (Page<ProjectInfo>) projectMapper.getPrjYs(query);
        return PageUtil.page2pager(page, query);

    }

    @Override
    public Pager getYyNew(Query query) {

        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ProjectInfo> page = (Page<ProjectInfo>) projectMapper.getYyNew(query);
        return PageUtil.page2pager(page, query);

    }

    @Override
    public Pager getBjNew(Query query) {

        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ProjectInfo> page = (Page<ProjectInfo>) projectMapper.getBjNew(query);
        return PageUtil.page2pager(page, query);

    }
}
